package com.xuanwu.ai.flow.controller;

import com.github.pagehelper.PageInfo;
import com.xuanwu.ai.flow.dto.FlowReqDTO;
import com.xuanwu.ai.flow.model.FlowChain;
import com.xuanwu.ai.flow.service.FlowManageServiceImpl;
import com.xuanwu.ai.jdbc.page.Pagination;
import com.xuanwu.ai.web.views.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(value = "流程管理相关接口", produces = "application/json")
@RestController
@RequestMapping(value = "/manage")
@Slf4j
public class FlowManageController {

    @Autowired
    private FlowManageServiceImpl flowManageService;


    @PostMapping("/saveFlow")
    @ApiOperation(value = "保存流程", notes = "保存流程")
    public ResponseVo saveFlow(@RequestBody FlowChain flowChain) {
        if (StringUtils.isEmpty(flowChain.getElData())) {
            return ResponseVo.fail("400", "流程规则不能为空");
        }
        flowManageService.saveFlow(flowChain);
        return ResponseVo.success();
    }


    @PostMapping("/getFlowList")
    @ApiOperation(value = "获取流程列表", notes = "获取流程列表")
    public ResponseVo<PageInfo<FlowChain>> getFlowList(@RequestBody FlowReqDTO flowReqDTO,
                                                       @RequestParam("pageNum") Integer pageNum,
                                                       @RequestParam("pageSize") Integer pageSize) {

        Pagination pagination = new Pagination(pageNum, pageSize);

        return ResponseVo.success(flowManageService.getFlowList(flowReqDTO, pagination));
    }


    @PostMapping("/getFlowDetail")
    @ApiOperation(value = "获取流程详情", notes = "获取流程详情")
    public ResponseVo<FlowChain> getFlowDetail(@RequestParam("flowId") Long flowId) {

        return ResponseVo.success(flowManageService.getFlowDetail(flowId));
    }


    @PostMapping("/updateFlow")
    @ApiOperation(value = "更新流程", notes = "更新流程")
    public ResponseVo updateFlow(@RequestBody FlowChain flowChain) {
        if (StringUtils.isEmpty(flowChain.getElData())) {
            return ResponseVo.fail("400", "流程规则不能为空");
        }
        flowManageService.updateFlow(flowChain);
        return ResponseVo.success();
    }


    @PostMapping("/deployFlow")
    @ApiOperation(value = "发布流程", notes = "发布流程")
    public ResponseVo deployFlow(@RequestParam("flowId") Long flowId) {
        if (StringUtils.isEmpty(flowId)) {
            return ResponseVo.fail("400", "流程规则ID不能为空");
        }
        flowManageService.deployFlow(flowId);
        return ResponseVo.success();
    }


}
